package com.cdc.views;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;

/**
 * ProjectName：CusetomView
 * DESC: (类描述)
 * Created by chendongchu 2019\1\7 0007
 * updateName:(修改人名称)
 * updateTime:(修改时间)11:50
 * updateDesc:(修改内容)
 */
public class TView extends View {
    private Paint paint;

    private int height;

    public TView(Context context) {
        this(context,null);
    }

    public TView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public TView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    private void init() {
        paint = new Paint();
       // paint.setAntiAlias(true);
        paint.setColor(Color.RED);
        paint.setStrokeWidth(3);
        paint.setStyle(Paint.Style.FILL);
       paint.setTextSize(120);


        mBounds = new Rect();


        Log.e(Temp.TAG, "==init===");
    }
    private String text="abcdefghijk";

    public void setText(String text){
        this.text=text;
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        paint.getTextBounds(text, 0, text.length(), mBounds);


        Paint.FontMetrics fontMetrics=paint.getFontMetrics();
        float ascent = fontMetrics.ascent;
        float descent = fontMetrics.descent;
        float top = fontMetrics.top;
        float bottom = fontMetrics.bottom;
        float leading = fontMetrics.leading;
        float baseLine=(descent-ascent)/2-descent;

        float baseLine2=height/2+(bottom-top)/2-bottom;

        canvas.drawText(text, 0, baseLine2, paint);
//Color.parseColor("#23AC3B")


        canvas.save();
        //canvas.restore();
        paint.setColor(Color.BLUE);

       // canvas.drawRect(mBounds,paint);
        canvas.drawLine(0, ascent, getWidth(), ascent, paint);
        canvas.save();
        paint.setColor(Color.YELLOW);
        canvas.drawLine(0, descent, getWidth(), descent, paint);
        canvas.save();
        paint.setColor(Color.GREEN);
        canvas.drawLine(0, top, getWidth(), top, paint);
        canvas.save();
        paint.setColor(Color.BLUE);
        canvas.drawLine(0, bottom, getWidth(), bottom, paint);
        canvas.save();
        paint.setColor(Color.YELLOW);
        canvas.drawLine(0, baseLine2, getWidth(), baseLine2, paint);

        canvas.save();
        canvas.restore();
      //  canvas.drawCircle(100,100,90,paint);

        Log.e(Temp.TAG, "==onDraw==="+"mBounds.height()="+height+",baseLine="+baseLine+",baseLine2="+baseLine2);




        Log.e(Temp.TAG, "ascent="+ascent+",descent="+descent+",top="+top+",bottom="+bottom+",leading="+leading);

    }

    private int mWidth;
    private int mHeight;
    private Rect mBounds;

   protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {


        int widthSize = measureWidth(widthMeasureSpec);



        int heightSize = measureHeight(heightMeasureSpec);

       height=heightSize;


        setMeasuredDimension(widthSize, heightSize);
        Log.e(Temp.TAG, "widthSize=" + widthSize + ",heightSize=" + heightSize);
    }

    private int measureHeight(int heightMeasureSpec){
        int result;
        int mode=MeasureSpec.getMode(heightMeasureSpec);
        int size=MeasureSpec.getSize(heightMeasureSpec);
        if(mode==MeasureSpec.EXACTLY){
            result=size;
        }else{
            result=getNeedHeight()+getPaddingTop()+getPaddingBottom();
            if(mode==MeasureSpec.AT_MOST){
                result=Math.min(result,size);
            }
        }
        return result;
    }

    private int getNeedHeight() {
        Paint.FontMetrics fontMetrics=paint.getFontMetrics();
        float ascent = fontMetrics.ascent;
        float descent = fontMetrics.descent;
        float top = fontMetrics.top;
        float bottom = fontMetrics.bottom;
        float leading = fontMetrics.leading;
        return (int) (bottom+descent-leading-top);
    }

    private int measureWidth(int widthMeasureSpec){
        int result=0;
        int mode=MeasureSpec.getMode(widthMeasureSpec);
        int size=MeasureSpec.getSize(widthMeasureSpec);
        if(mode==MeasureSpec.EXACTLY){
            result=size;
        }else{
            result=getNeedWidth()+getPaddingTop()+getPaddingBottom();
            if(mode==MeasureSpec.AT_MOST){
                result=Math.min(result,size);
            }
        }
        return result;
    }

    private int getNeedWidth() {
       String text="abcdefghijk";
        paint.getTextBounds(text, 0, text.length(), mBounds);
        Log.e(Temp.TAG,mBounds.height()+"=height");
        return mBounds.width();
    }
}
